package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;

import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void delete(Long id) {
        //删除关系
        employeeMapper.deleteRelation(id);

        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Employee employee, Long[] ids) {

        //对密码进行加密(把用户名当做盐)
        Md5Hash md5Hash = new Md5Hash(employee.getPassword(), employee.getName());
        employee.setPassword(md5Hash.toString());
        //保存到数据库
        employeeMapper.insert(employee);

        //关联关系
       /* if(ids!=null&&ids.length>0){
            for (Long rid : ids) {
                employeeMapper.insertRelation(employee.getId(),rid);
            }
        }*/

        //批量插入的方式
        if(ids!=null&&ids.length>0) {
            employeeMapper.insertBatchRelation(employee.getId(),ids);
        }

    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);

        //删除关系
        employeeMapper.deleteRelation(employee.getId());

        //批量插入的方式
        if(ids!=null&&ids.length>0) {
            employeeMapper.insertBatchRelation(employee.getId(),ids);
        }

    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> employees = employeeMapper.selectForList(qo);
        return  new PageInfo<>(employees);
    }

    @Override
    public Employee login(String username, String password) {
        //查询数据库中是否有匹配的数据
        Employee employee = employeeMapper.selectByUsernameAndPassword(username,password);
        if(employee==null){
            //通知调用者出现异常
            throw new RuntimeException("账号和密码不匹配");
        }
        return employee;

    }

    @Override
    public void batchDelete(Long[] ids) {
        employeeMapper.batchDelete(ids);

        //删除中间表数据

    }

    @Override
    public Employee selectByName(String name) {
        return employeeMapper.selectByName(name);
    }

    @Override
    public Workbook exportXls() {
        //获取所有的员工数据
        List<Employee> employees = employeeMapper.selectAll();
        //创建一个excel对象
        Workbook workbook = new HSSFWorkbook();
        //创建一页纸
        Sheet sheet = workbook.createSheet("员工通讯录");
        //创建标题行
        Row row = sheet.createRow(0);
        //创建单元格(传入列，索引从0),在单元格上写内容
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");

        //遍历获取每一个员工
        for (int i = 0; i < employees.size(); i++) {
            Employee employee = employees.get(i);
            //创建一行(索引从0)
            row = sheet.createRow(i+1);
            //创建单元格(传入列，索引从0),在单元格上写内容
            row.createCell(0).setCellValue(employee.getName());
            row.createCell(1).setCellValue(employee.getEmail());
            if(employee.getAge()!=null){
                row.createCell(2).setCellValue(employee.getAge());
            }

        }
        return workbook;
    }

    @Override
    public void importXls(MultipartFile file) throws IOException {
        //读excel文件
        Workbook workbook = new HSSFWorkbook(file.getInputStream());
        //读哪一页数据
        Sheet sheet = workbook.getSheetAt(0);
        //获取最后一行的行号
        int lastRowNum = sheet.getLastRowNum();
        //标题行不需要读
        for (int i = 1; i <= lastRowNum; i++) {
            //获取每一行
            Row row = sheet.getRow(i);
            //创建员工对象
            Employee employee = new Employee();
            String name = row.getCell(0).getStringCellValue();
            if(!StringUtils.hasLength(name)) { //字符串空值判断
               return;  //continue;
            }
            employee.setName(name); //用户名
            String email = row.getCell(1).getStringCellValue();
            employee.setEmail(email); //邮箱
            //判断单元格的类型
           /* CellType cellType = row.getCell(2).getCellType();
            if(cellType.equals(CellType.STRING)){  //文本类型单元格
                String age = row.getCell(2).getStringCellValue(); //getStringCellValue用来读文本类型的单元格
                employee.setAge(Integer.valueOf(age)); //年龄
            }else{
                double value = row.getCell(2).getNumericCellValue(); //getNumericCellValue用来读数值类型的单元格
                employee.setAge((int)value); //年龄
            }*/
            employee.setPassword("1");
            //插入到数据库
            this.save(employee,null);
        }



    }

    @Override
    public List<Employee> selectByRoleSn(String... sns) {
        return employeeMapper.selectByRoleSn(sns);
    }
}
